package com.tech.koufu.ui.adapter;

import java.util.ArrayList;

import org.json.JSONObject;

import com.tech.koufu.R;
import com.tech.koufu.model.EntrustMsg;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.adapter.TradeAdapter.ViewHolder;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
 * 用户消息
 * @author jl_luo
 *
 */
public class EntrustMsgAdapter extends BaseAdapter {
	private Context context;  
	private LayoutInflater mInflater = null; 
	public ArrayList<EntrustMsg> datas = null;
	public EntrustMsgAdapter(Context context){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		 if(datas!=null){
			 return datas.size();
		 }
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		if(datas!=null){
			return datas.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_msg, parent,false);  
             holder.msgDate = (TextView)convertView.findViewById(R.id.txt_msg_date);  
             holder.msgContent = (TextView)convertView.findViewById(R.id.txt_msg_content);   
             holder.msgStock = (TextView)convertView.findViewById(R.id.txt_msg_target);
             holder.msgPrice = (TextView)convertView.findViewById(R.id.txt_msg_option);//买入价还是卖出 
             holder.ivTop = (View)convertView.findViewById(R.id.iv_top);
             holder.ivBottom = (View)convertView.findViewById(R.id.iv_bottom);
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         EntrustMsg msg = datas.get(position);
         holder.msgDate.setText(msg.date);
         String bs = msg.entrust_bs;
         String content = msg.username+KouFuTools.getEntrustType(bs)+"股票";
         SpannableStringBuilder ssb =  new SpannableStringBuilder(content);
         ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.TextColorRed_FD0000))
         							,msg.username.length(),msg.username.length()+KouFuTools.getEntrustType(bs).length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         holder.msgContent.setText(ssb);
         holder.msgStock.setText(msg.stock_name+"("+msg.stock_code+")");
         String pr = "";
         if(bs.equals("c")){
        	 pr = "卖出价："+msg.price;
         }else if(bs.equals("r")){
        	 pr = "买入价："+msg.price;
         }
         ssb =  new SpannableStringBuilder(pr);
         ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.TextColorRed_FD0000))
         							,4,pr.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         holder.msgPrice.setText(ssb); 
         
         if(position==0){
        	 holder.ivTop.setVisibility(View.INVISIBLE);
         }else if(position == datas.size()-1){
        	 holder.ivBottom .setVisibility(View.INVISIBLE);
         }else{
        	 holder.ivTop.setVisibility(View.VISIBLE);
        	 holder.ivBottom.setVisibility(View.VISIBLE);
         }
		return convertView;
	}

	static class ViewHolder  
    {   
		 
        public TextView msgDate;  
        public TextView msgContent;   
        public TextView msgStock;
        public TextView  msgPrice; 
        public View ivTop;
        public View ivBottom;
        
    }  
}
